package org.come.Jpanel;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.cbg.until.SrcollPanelUINew;
import org.come.Frame.ImpactGradeJframe;
import org.come.bean.ChongjipackBean;
import org.come.bean.ConfigureBean;
import org.come.bean.LoginResult;
import org.come.model.Configure;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;
import com.updateNew.MyIsif;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.VipShopBtn;
import com.tool.role.RoleData;

/**
 * HGC-2019-11-22 冲级奖励主面板
 *
 * @author Administrator
 *
 */
public class ImpactGradeJpanel extends JPanel {

	/**
	 * 免费冲级菜单按钮freeMenuBtn<br>
	 * 小资冲级菜单按钮smallMenuBtn<br>
	 * 豪华冲级菜单按钮luxuryMenuBtn<br>
	 */
	private VipShopBtn freeMenuBtn, smallMenuBtn, luxuryMenuBtn, meiRBtn, LXBtn,LJBtn;
	private List<ImpactGradeGoodsJpanel> goodsJpanels;
	private JScrollPane scrollPane;

	private List<ChongjipackBean> chongjipackBeans;

	public ImpactGradeJpanel() {
		ConfigureBean allConfigure = UserMessUntil.getConfigureBean();
		Map<BigDecimal, Configure> item = allConfigure.getAllConfigure();
		Configure configure = item.get(new BigDecimal(7));
		setPreferredSize(new Dimension(735, 451));
		setOpaque(false);
		setLayout(null);

		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/newbutton/cjlbgb.png", 1, 103);
		offBtn.setBounds(735 - 31, 22, 30, 30);
		this.add(offBtn);

		getFreeMenuBtn();
		getSmallMenuBtn();
		getLuxuryMenuBtn();
//		getMeiRiBtn();//按钮屏蔽
//		getLXBtn();//按钮屏蔽
//		getLJBtn();//按钮屏蔽
		getScrollPane();
		getGoodsJpanels();
		this.typeMenu=1;
		getGoods();
	}

	/** 1.免费 2 小资 3土豪 */
	private int typeMenu = -1;

	public void changeMenuBtn(int type) {
		if (type == -1) {
			type = 1;
		}

		typeMenu = type;
		try {
			if (typeMenu == 1) {
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
//				LJBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K96.png"));
				freeMenuBtn.imgchange(2);
				smallMenuBtn.imgchange(0);
				luxuryMenuBtn.imgchange(0);
//				meiRBtn.imgchange(0);
//				LXBtn.imgchange(0);
//				LJBtn.imgchange(0);
			} else if (typeMenu == 2) {
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
				freeMenuBtn.imgchange(0);
				smallMenuBtn.imgchange(2);
				luxuryMenuBtn.imgchange(0);
//				meiRBtn.imgchange(0);
//				LXBtn.imgchange(0);
//				LJBtn.imgchange(0);
			} else if (typeMenu == 3) {
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
				freeMenuBtn.imgchange(0);
				smallMenuBtn.imgchange(0);
				luxuryMenuBtn.imgchange(2);
//				meiRBtn.imgchange(0);
//				LXBtn.imgchange(0);
//				LJBtn.imgchange(0);
			} else if (typeMenu == 4) {
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
//				LJBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K96.png"));
				freeMenuBtn.imgchange(0);
				smallMenuBtn.imgchange(0);
				luxuryMenuBtn.imgchange(0);
//				meiRBtn.imgchange(2);
//				LXBtn.imgchange(0);
//				LJBtn.imgchange(0);
			} else if (typeMenu == 5) {
//				iconTop = CutButtonImage.getImage("inkImg/background/7112.png", -1, -1);        //礼包中心控制 连续充值
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
//				LJBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K96.png"));
				freeMenuBtn.imgchange(0);
				smallMenuBtn.imgchange(0);
				luxuryMenuBtn.imgchange(0);
//				meiRBtn.imgchange(0);
//				LXBtn.imgchange(2);
//				LJBtn.imgchange(0);
			} else if (typeMenu == 7) {
				freeMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K88.png"));
				smallMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K90.png"));
				luxuryMenuBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K92.png"));
//				meiRBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K93.png"));
//				LXBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K95.png"));
//				LJBtn.setIcons(CutButtonImage.cuts("inkImg/newbutton/K96.png"));
				freeMenuBtn.imgchange(0);
				smallMenuBtn.imgchange(0);
				luxuryMenuBtn.imgchange(0);
//				meiRBtn.imgchange(0);
//				LXBtn.imgchange(0);
//				LJBtn.imgchange(2);
			}
			scrollPane.getVerticalScrollBar().setValue(0);

			getGoods();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void getGoods() {
		// (注:type 表示等级 1.免费 2 小资 3土豪)
		if (typeMenu == -1)
			return;
		String sendmes = Agreement.getAgreement().chongjipackgetAgreement(typeMenu + "");
		SendMessageUntil.toServer(sendmes);
	}

	private String getLiBaoType(ChongjipackBean chongjipackBean) {
		if (chongjipackBean.getPacktype() == 1) {
			return "4";
		}
		if (chongjipackBean.getPacktype() == 2 || chongjipackBean.getPacktype() == 3) {
			return "5";
		} else if (chongjipackBean.getPacktype() == 4) {
			return "2";
		} else if (chongjipackBean.getPacktype() == 5) {
			return "3";
		} else if (chongjipackBean.getPacktype() == 7) {
			return "7";
		}
		return "";

	}

	public void showGoods(List<ChongjipackBean> chongjipackBeans) {
		if (chongjipackBeans == null) {
			chongjipackBeans = new ArrayList<>();
		}
		goodsJpanels.clear();
		this.chongjipackBeans = chongjipackBeans;
		String vipget = RoleData.getRoleData().getLoginResult().getVipget();
		String[] split = null;
		if (vipget != null) {
			split = vipget.split("&&");
		}
		for (int i = 0; i < chongjipackBeans.size(); i++) {
			if (i < chongjipackBeans.size()) {
				if (goodsJpanels.size() < chongjipackBeans.size()) {
					goodsJpanels.add(new ImpactGradeGoodsJpanel());
				}
				LoginResult loginResult = RoleData.getRoleData().getLoginResult();
				int needlv = AnalysisString.lvldirection1(chongjipackBeans.get(i).getPackgrade());
				int level = loginResult.getGrade();
				BigDecimal daypaysum = loginResult.getDaypaysum();// 日累计充值
				BigDecimal paysum = loginResult.getPaysum();// 累计充值
				BigDecimal dayandpayorno = loginResult.getDayandpayorno();// 连续充值数
				if (chongjipackBeans.get(i).getPacktype() == 1 || chongjipackBeans.get(i).getPacktype() == 2 || chongjipackBeans.get(i).getPacktype() == 3) {
					if (ImpactGradeGoodsJpanel.checkYesOrNo(split, getLiBaoType(chongjipackBeans.get(i)), chongjipackBeans.get(i).getPackgradetype() + "")) {
						goodsJpanels.get(i).getNameRich().setText(chongjipackBeans.get(i).getPackgrade() + "级");
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(false);
					} else if (level < needlv) {
						goodsJpanels.get(i).getNameRich().setText(chongjipackBeans.get(i).getPackgrade() + "级");
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(true);
						goodsJpanels.get(i).getGetBtn().setIcon(CutButtonImage.getImage("inkImg/newbutton/99.png", -1, -1));
						goodsJpanels.get(i).getGetBtn().setBounds(420, 3, 85, 32);
					} else {
						goodsJpanels.get(i).getGetBtn().setBtn(1);
						try {
							goodsJpanels.get(i).getNameRich().setText(chongjipackBeans.get(i).getPackgrade() + "级");
							goodsJpanels.get(i).getGetBtn().setVisible(true);
							goodsJpanels.get(i).getGetBtn().setIcons(CutButtonImage.cuts("inkImg/newbutton/191.png"));
							goodsJpanels.get(i).getGetBtn().setBounds(404, 12, 85, 32);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}else if(chongjipackBeans.get(i).getPacktype() == 4){
					if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "2", chongjipackBeans.get(i).getPackgradetype() + "")) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(false);
					} else if (daypaysum.doubleValue() < chongjipackBeans.get(i).getCanpaymoney()) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(true);
						goodsJpanels.get(i).getGetBtn().setIcon(CutButtonImage.getImage("inkImg/newbutton/99.png", -1, -1));
						goodsJpanels.get(i).getGetBtn().setBounds(420, 3, 85, 32);
					} else {
						goodsJpanels.get(i).getGetBtn().setBtn(1);
						try {
							goodsJpanels.get(i).getGetBtn().setVisible(true);
							goodsJpanels.get(i).getGetBtn().setIcons(CutButtonImage.cuts("inkImg/newbutton/191.png"));
							goodsJpanels.get(i).getGetBtn().setBounds(404, 12, 85, 32);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}else if(chongjipackBeans.get(i).getPacktype() == 5){
					if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "3", chongjipackBeans.get(i).getPackgradetype() + "")) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(false);
					} else if (dayandpayorno.compareTo(BigDecimal.valueOf(chongjipackBeans.get(i).getPackgradetype())) < 0) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(true);
						goodsJpanels.get(i).getGetBtn().setIcon(CutButtonImage.getImage("inkImg/newbutton/99.png", -1, -1));
						goodsJpanels.get(i).getGetBtn().setBounds(420, 3, 85, 32);
					} else {
						goodsJpanels.get(i).getGetBtn().setBtn(1);
						try {
							goodsJpanels.get(i).getGetBtn().setVisible(true);
							goodsJpanels.get(i).getGetBtn().setIcons(CutButtonImage.cuts("inkImg/newbutton/191.png"));
							goodsJpanels.get(i).getGetBtn().setBounds(404, 12, 85, 32);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}else {
					if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "7", chongjipackBeans.get(i).getPackgradetype() + "")) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(false);
					} else if (paysum.doubleValue() < chongjipackBeans.get(i).getCanpaymoney()) {
						goodsJpanels.get(i).getGetBtn().setBtn(-1);
						goodsJpanels.get(i).getGetBtn().setVisible(true);
						goodsJpanels.get(i).getGetBtn().setIcon(CutButtonImage.getImage("inkImg/newbutton/99.png", -1, -1));
						goodsJpanels.get(i).getGetBtn().setBounds(420, 3, 85, 32);
					} else {
						goodsJpanels.get(i).getGetBtn().setBtn(1);
						try {
							goodsJpanels.get(i).getGetBtn().setVisible(true);
							goodsJpanels.get(i).getGetBtn().setIcons(CutButtonImage.cuts("inkImg/newbutton/191.png"));
							goodsJpanels.get(i).getGetBtn().setBounds(404, 12, 85, 32);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				if (chongjipackBeans.get(i).getPacktype() == 1 || chongjipackBeans.get(i).getPacktype() == 2 || chongjipackBeans.get(i).getPacktype() == 3) {
					goodsJpanels.get(i).showGoods(chongjipackBeans.get(i));
				}else {
					goodsJpanels.get(i).showGoods1(chongjipackBeans.get(i));
				}
			} else {
				goodsJpanels.get(i).showGoods(null);
			}
		}
		addGoods();
	}
	/**
	 * 刷新面板
	 */
	public void refreshPanel() {
		showGoods(chongjipackBeans);
	}

	/**
	 * 滚动条添加物品
	 */
	public void addGoods() {
		JPanel view = (JPanel) scrollPane.getViewport().getView();
		view.removeAll();
		view.repaint();
		int num = 0;
		for (int i = 0; i < goodsJpanels.size(); i++) {
			ImpactGradeGoodsJpanel vipGoodsJpanel = goodsJpanels.get(i);
			if (vipGoodsJpanel.getChongjipackBean() == null) {
				continue;
			}
			vipGoodsJpanel.setBounds(10, num * 62, 477, 57);
			num++;
			view.add(vipGoodsJpanel);
		}
		view.setPreferredSize(new Dimension(477, 62 * num));
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();

		if (!ImpactGradeJframe.getImpactGradeJframe().isVisible()) {
			FormsManagement.showForm(103);
		}
	}

	private ImageIcon iconBack;
	private ImageIcon iconTop;
	private ImageIcon iconshj;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);

		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/newbackground/B325.png", -1, -1);
		}
		g.drawImage(iconBack.getImage(), 0, 0, 735, 451, this);

		if (iconTop == null) {
			iconTop = CutButtonImage.getImage("inkImg/newbackground/B334.png", -1, -1);
		}
		g.drawImage(iconTop.getImage(), 205, 50, 491, 89, this);
		if (iconshj == null) {
			iconshj = CutButtonImage.getImage("inkImg/newbackground/b335.png", -1, -1);
		}
		g.drawImage(iconshj.getImage(), 205, 119, 491, 21, this);
	}
	public VipShopBtn getFreeMenuBtn() {
		if (freeMenuBtn == null) {
			freeMenuBtn = new VipShopBtn("inkImg/newbutton/K88.png", 1, 5, this);
			freeMenuBtn.setBounds(88, 70, 104, 28);
			this.add(freeMenuBtn);
		}
		return freeMenuBtn;
	}
	public void setFreeMenuBtn(VipShopBtn freeMenuBtn) {
		this.freeMenuBtn = freeMenuBtn;
	}

	public VipShopBtn getSmallMenuBtn() {
		if (smallMenuBtn == null) {
			smallMenuBtn = new VipShopBtn("inkImg/newbutton/K90.png", 1, 6, this);
			smallMenuBtn.setBounds(90, 70+35, 104, 28);
			this.add(smallMenuBtn);
		}
		return smallMenuBtn;

	}
	public void setSmallMenuBtn(VipShopBtn smallMenuBtn) {
		this.smallMenuBtn = smallMenuBtn;
	}

	public VipShopBtn getLuxuryMenuBtn() {
		if (luxuryMenuBtn == null) {
			luxuryMenuBtn = new VipShopBtn("inkImg/newbutton/K92.png", 1, 7, this);
			luxuryMenuBtn.setBounds(90, 70+35*2, 104, 28);
			this.add(luxuryMenuBtn);
		}
		return luxuryMenuBtn;
	}
	public void setLuxuryMenuBtn(VipShopBtn luxuryMenuBtn) {
		this.luxuryMenuBtn = luxuryMenuBtn;
	}

	public VipShopBtn getMeiRiBtn() {//每日充值
		if (meiRBtn == null) {
			meiRBtn = new VipShopBtn("inkImg/newbutton/K93.png", 1, 9, this);
			meiRBtn.setBounds(90, 70 + 35*3, 104, 28);
			this.add(meiRBtn);
		}
		return meiRBtn;
	}
	public void setMeiRBtn(VipShopBtn meiRBtn) {
		this.meiRBtn = meiRBtn;
	}
	public VipShopBtn getLXBtn() {//连续充值
		if (LXBtn == null) {
			LXBtn = new VipShopBtn("inkImg/newbutton/K95.png", 1, 10, this);
			LXBtn.setBounds(90, 70 + 35 * 4, 104, 28);
			this.add(LXBtn);
		}
		return LXBtn;
	}
	public void setLXBtn(VipShopBtn LXBtn) {
		this.LXBtn = LXBtn;
	}
	public VipShopBtn getLJBtn() {//累计充值
		if (LJBtn == null) {
			LJBtn = new VipShopBtn("inkImg/newbutton/K96.png", 1, 12, this);
			LJBtn.setBounds(90, 70 + 35 * 5, 104, 28);
			this.add(LJBtn);
		}
		return LJBtn;
	}
	public void setLJBtn(VipShopBtn LJBtn) {
		this.LJBtn = LJBtn;
	}
	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public ImageIcon getIconTop() {
		return iconTop;
	}

	public void setIconTop(ImageIcon iconTop) {
		this.iconTop = iconTop;
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			// 属性滚动条
			scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUINew());
			scrollPane.getVerticalScrollBar().setUnitIncrement(20);
			scrollPane.getViewport().setOpaque(false);
			scrollPane.setOpaque(false);
			scrollPane.setBounds(197, 177, 503, 250);
			scrollPane.setBorder(BorderFactory.createEmptyBorder());
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			JPanel jPanel = new JPanel();
			jPanel.setBackground(null);
			jPanel.setOpaque(false);
			jPanel.setBorder(null);
			jPanel.setLayout(null);
			scrollPane.setViewportView(jPanel);
			this.add(scrollPane);

		}
		return scrollPane;
	}
	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	public List<ImpactGradeGoodsJpanel> getGoodsJpanels() {
		if (goodsJpanels == null) {
			goodsJpanels = new ArrayList<>();
			for (int i = 0; i < 10; i++) {
				goodsJpanels.add(new ImpactGradeGoodsJpanel());
			}
		}
		return goodsJpanels;
	}

	public void setGoodsJpanels(List<ImpactGradeGoodsJpanel> goodsJpanels) {
		this.goodsJpanels = goodsJpanels;
	}

	public int getTypeMenu() {
		return typeMenu;
	}

	public void setTypeMenu(int typeMenu) {
		this.typeMenu = typeMenu;
	}

}
